UiHelper.frame = obj:extend()

--- tag方式新建一个frame
---@param fdfType string frame类型 TEXT | BACKDROP等
---@param fdfName string frame名称
---@param parent number 父节点ID(def:GameUI)
---@param tag string 自定义tag名称(def:cache.tagIdx)
---@param id number integer(def:0)
function UiHelper.frame:new(args)
    self.fdfType = args.fdfType
    self.fdfName = args.fdfName or ""
    self.parent = args.parent or hdzui.origin.game()
    self.tag = args.tag
    self.id = args.id or 0
    self.w = args.w or 0.1
    self.h = args.h or 0.1
    self.style = args.style
    self.x = args.x or 0
    self.y = args.y or 0
    self.relation = args.relation or hdzui.origin.game()
    self.align = args.align or FRAME_ALIGN_CENTER
    self.alignRelation = args.alignRelation or FRAME_ALIGN_CENTER

    if args.show == nil then
        self.isShow = true
    else
        self.isShow = args.show
    end
    
    self.frameId = hdzui.frameTag(self.fdfType, self.fdfName, self.parent, self.tag, self.id)


    if args.w ~= nil and args.h ~= nil then
        self:size(self)
    end

    self:dfshow()
    self:framePoint(self)
    return self
end
function UiHelper.frame:size(args)
    hjapi.DzFrameSetSize(self.frameId, args.w, args.h)
end

---清空frame所有锚点
function UiHelper.frame:dfClearAllPoints()
    hjapi.DzFrameClearAllPoints(self.frameId)
    return self
end
---显示
function UiHelper.frame:dfshow()
    hjapi.DzFrameShow(self.frameId, true)
    return self
end
---隐藏
function UiHelper.frame:dfhide()
    hjapi.DzFrameShow(self.frameId, false)
    return self
end

---设置相对位置{point,relativeFrame,relativePoint}
function UiHelper.frame:SetPoint(args)
    self.x = args.x or 0
    self.y = args.y or 0
    self.point = args.point
    self.relativeFrame = args.relativeFrame
    self.relativePoint = args.relativePoint
    hjapi.DzFrameSetPoint(self.frameId, self.point, self.relativeFrame, self.relativePoint, self.x, self.y)
end

---偏移
---@param relation number-相对节点ID(def:hdzui.origin.game())
---@param x number-锚点X
---@param y number-锚点Y
function UiHelper.frame:framePoint(args)
    self.x = args.x
    self.y = args.y
    self.relation = args.relation
    hdzui.framePoint(self.frameId, self.relation, self.align, self.alignRelation, args.x, args.y)
end






---设置宽高
function UiHelper.frame:dfsize(w, h)
    hjapi.DzFrameSetSize(self.frameId, w, h)
    return self
end

---设置锚点 相对位置{point,relativeFrame,relativePoint}
function UiHelper.frame:dfpoint(x, y, point, relativeFrame, relativePoint)
    hjapi.DzFrameSetPoint(self.frameId, point, relativeFrame, relativePoint, x, y)
    return self
end

---偏移
---@param relation number-相对节点ID(def:hdzui.origin.game())
---@param x number-锚点X
---@param y number-锚点Y
function UiHelper.frame:dfpointp(x,y,relation,align,alignRelation)
    relation=relation or  hdzui.origin.game()
    hdzui.framePoint(self.frameId, relation, align, alignRelation, x, y)
    return self
end


---设置frame贴图 支持Backdrop、SimpleStatusBar 平铺
---@param img string-图片路径
function UiHelper.frame:dfbackdrop(img)
    hjapi.DzFrameSetTexture(self.frameId, img, 0)
    return self
end

---设置frame贴图 支持Backdrop、SimpleStatusBar 平铺
---@param img string-图片路径
---@param frameId number-指定id修改图片
function UiHelper.frame:dfbackdropid(frameId,img)
    hjapi.DzFrameSetTexture(frameId, img, 0)
    return self
end

---设置frame文本 支持EditBox, TextFrame, TextArea, SimpleFontString、GlueEditBoxWar3、SlashChatBox、TimerTextFrame、TextButtonFrame、GlueTextButton
function UiHelper.frame:dftext(text)
    hjapi.DzFrameSetText(self.frameId, text)
    return self
end

---指定id设置frame文本 支持EditBox, TextFrame, TextArea, SimpleFontString、GlueEditBoxWar3、SlashChatBox、TimerTextFrame、TextButtonFrame、GlueTextButton
function UiHelper.frame:dftextid(frameId,text)
    hjapi.DzFrameSetText(frameId, text)
    return self
end

--设置绝对位置
function UiHelper.frame:dfAbsolutePoint(x, y, point)
    hjapi.DzFrameSetAbsolutePoint(self.frameId, point, x, y)
    return self
end

--- 设置字体 设置 frame 的字体为 font, 大小 height, flag flag 支持EditBox、SimpleFontString、SimpleMessageFrame以及非SimpleFrame类型的例如TEXT，flag作用未知
function UiHelper.frame:dfsetStyle(family,size,flag)
    family=family or "fonts.ttf"
    size=size or 8 * 0.001
    flag=flag or 0
   
    hjapi.DzFrameSetFont(self.frameId, family , size, flag)
    return self
end

---设置frame对齐方式 支持TextFrame、SimpleFontString、SimpleMessageFrame
function UiHelper.frame:dfsetTextAlignment(align)
    align =align or TEXT_ALIGN_LEFT
    hjapi.DzFrameSetTextAlignment(self.frameId, align)
    return self
end

---点击事件
function UiHelper.frame:CLICK(align)
    if (align ~= nil and type(align) == "function") then
        hdzui.onMouse(self.frameId, MOUSE_ORDER_CLICK, align)
    end
    return self
end

---悬浮事件
function UiHelper.frame:ENTER(align)
    if (align ~= nil and type(align) == "function") then
        hdzui.onMouse(self.frameId, MOUSE_ORDER_ENTER, align)
    end
    return self
end


---离开事件
function UiHelper.frame:LEAVE(align)
    if (align ~= nil and type(align) == "function") then
        hdzui.onMouse(self.frameId, MOUSE_ORDER_LEAVE, align)
    end
    return self
end

---释放事件
function UiHelper.frame:RELEASE(align)
    if (align ~= nil and type(align) == "function") then
        hdzui.onMouse(self.frameId, MOUSE_ORDER_RELEASE, align)
    end
    return self
end

--滚动事件
function UiHelper.frame:SCROLL(align)
    if (align ~= nil and type(align) == "function") then
        hdzui.onMouse(self.frameId, MOUSE_ORDER_SCROLL, align)
    end
    return self
end

--双击事件
function UiHelper.frame:DOUBLE_CLICK(align)
    if (align ~= nil and type(align) == "function") then
        hdzui.onMouse(self.frameId, MOUSE_ORDER_DOUBLE_CLICK, align)
    end
    return self
end
